--根据用户的设置创建任务列表
require("doTaskPrepare");
require("doTask");
require("judge");

--任务栈 先进先出
local onlyTaskArray = {};
local timeTaskArray = {};
local taskArray = {};

--空闲时
local otherTaskArray = {};


--根据用户设置，创建任务列表
function createTaskListWithSetting(setting)
  
  dealCooperateTaskType = tonumber(setting["协作任务_set"]);
  
  if setting["组队时邀请好友_set"] == "0" then
    shouldInvateFriend = true;
  end
  
	friendKind = tonumber(setting["好友类型_set"]);
  
  dealPowerEmptyType = tonumber(setting["体力策略_set"]);
  
  
  if setting["自动换狗粮_set"] == "0" then
    autoChangeFull = true;
  else
    autoChangeFull = false;
  end
  
  autoChangeFullType = tonumber(setting["自动换狗粮_type"]);
  changeType = tonumber(setting["自动换狗粮_changeType"]);
  scrollTimes = tonumber(setting["自动换狗粮_scrollTimes"]);
  scrollSpeed = tonumber(setting["自动换狗粮_scrollSpeed"]);
	autoChangeStarLevel = tonumber(setting["自动换狗粮_changeStarLevel"]);
  
  if setting["只做封任务_name"] == "0" then
    local task = {};
    task.name = "只做封任务";
    task.isWorking = false;
    --最高章节
    local chapterNumber = tonumber(setting["只做封任务_chapter"]) + 1;
    local chapter = covertNumberToMonsterChapter(chapterNumber);
    task.chapter = chapter;
    
    local attackBoss = true;
    if setting["只做封任务_attackBoss"] == "0" then
      attackBoss = false;
    end
    task.attackBoss = attackBoss;
    
    local makeTeam = false;
    if setting["只做封任务_makeTeam"] == "0" then
      makeTeam = true;
    else
      makeTeam = false;
    end
    task.makeTeam = makeTeam;
    
    --要打的怪的类型
    task.fightType = "打任务怪";
    
    --本章是否有任务怪 默认没有
    task.hasTask = false;
    task.beginTime = os.time();
    
    table.insert(onlyTaskArray, task);
  end
  
  if setting["妖怪发现_name"] == "0" then
    
    local name = "妖怪发现";
    local times = tonumber(setting["妖怪发现_times"]);
    
    local chapterNumber = tonumber(setting["妖怪发现_chapter"]) + 1;
    local chapter = covertNumberToMonsterChapter(chapterNumber);
    
    local makeTeam = false;
    if setting["妖怪发现_makeTeam"] == "0" then
      makeTeam = true;
    end
    
    local attackBoss = true;
    if setting["妖怪发现_attackBoss"] == "0" then
      attackBoss = false;
    end
    
    local fightType = "打全部";
    if setting["妖怪发现_fightType"] == "0" then
      fightType = "打全部";
    elseif setting["妖怪发现_fightType"] == "1" then
      fightType = "打经验怪";
    elseif setting["妖怪发现_fightType"] == "2" then
      fightType = "打金钱怪";
    elseif setting["妖怪发现_fightType"] == "3" then
      fightType = "打经验和金钱怪";
    end
    
    local makeTeam = false;
    if setting["妖怪发现_makeTeam"] == "0" then
      makeTeam = true;
    else
      makeTeam = false;
    end
    
    if times ~= 0 then
      local task = {};
      task.name = name;
      task.isWorking = false;
      task.times = times;
      task.chapter = chapter;
      task.fightType = fightType;
      task.attackBoss = attackBoss;
      task.makeTeam = makeTeam;
      task.beginTime = os.time();
      table.insert(taskArray, task);
    end
    
  end
  
  if setting["八岐大蛇_name"] == "0" then
    local times = tonumber(setting["八岐大蛇_times"]);
    if times ~= 0 then
      local task = {};
      task.name = "八岐大蛇";
      task.isWorking = false;
      task.times = times;
      task.beginTime = os.time();
      
      local chapterNumber = tonumber(setting["八岐大蛇_chapter"]) + 1;
      task.chapter = covertNumberToYuhunJuexingChapter(chapterNumber);
      
      local makeTeam = false;
      if setting["八岐大蛇_makeTeam"] == "0" then
        task.makeTeam = true;
      else
        task.makeTeam = false;
      end
      
      
      local taskminLevel = tonumber(setting["八岐大蛇_minLevel"]);
      task.taskminLevel = taskminLevel;
      
      table.insert(taskArray, task);
    end
  end
  
  if setting["打业原火_name"] == "0" then
    local times = tonumber(setting["打业原火_times"]);
    if times ~= 0 then
      local task = {};
      task.name = "打业原火";
      task.isWorking = false;
      task.times = times;
      task.beginTime = os.time();
      
      local chapterNumber = tonumber(setting["打业原火_chapter"]) + 1;
      task.chapter = convertNumberToYeyuanhuoChapter(chapterNumber);
      
      table.insert(taskArray, task);
    end
  end
  
  
  if setting["自动觉醒_name"] == "0" then
    local times = tonumber(setting["自动觉醒_times"]);
    if times ~= 0 then
      local task = {};
      task.name = "自动觉醒";
      task.isWorking = false;
      task.times = times;
      task.beginTime = os.time();
      
      local taskTypeNumber = tonumber(setting["自动觉醒_type"]) + 1;
      task.taskType = convertNumberToJuexingType(taskTypeNumber);
      
      local chapterNumber = tonumber(setting["自动觉醒_chapter"]) + 1;
      task.chapter = covertNumberToYuhunJuexingChapter(chapterNumber);
      
      
      local makeTeam = false;
      if setting["自动觉醒_makeTeam"] == "0" then
        task.makeTeam = true;
      else
        task.makeTeam = false;
      end
      
      local taskminLevel = tonumber(setting["自动觉醒_minLevel"]);
      task.taskminLevel = taskminLevel;
      
      table.insert(taskArray, task);
    end
  end
  
  
  -------------------挤队伍---------
  --自动石距
  if setting["自动石距_name"] == "0" then
    local task = {};
    task.name = "自动石距";
    task.teamArray = {};
    task.isWorking = false;
    
    if setting["自动石距_beginType"] == "0" then
      --未打过
      task.beginTime = os.time();
      
    elseif setting["自动石距_beginType"] == "1" then
      --已打过
      task.beginTime = os.time() + 60 * 60;
    end
    
    table.insert(timeTaskArray, task);
  end
  
  --自动年兽
  if setting["自动年兽_name"] == "0" then
    local task = {};
    task.name = "自动年兽";
    task.teamArray = {};
    task.isWorking = false;
    
    if setting["自动年兽_beginType"] == "0" then
      --未打过
      task.beginTime = os.time();
      
    elseif setting["自动年兽_beginType"] == "1" then
      --已打过
      task.beginTime = os.time() + 60 * 60 * 60;
    end
    
    table.insert(timeTaskArray, task);
  end
  
  
  --自动斗技
  if setting["自动斗技_name"] == "0" then
    
    local times = tonumber(setting["自动斗技_times"]);
    
    if times > 0 then
      
      local task = {};
      task.name = "自动斗技";
      task.isWorking = false;
      task.times = times;
      
      --dealType:0 自动斗技  1 无限退出
      if setting["自动斗技_dealType"] == "0" then
        --自动斗技
        task.dealType = 0;
      else
        --无限退出
        task.dealType = 1;
      end
      
      local hour = tonumber(os.date("%H"));
      local minute = tonumber(os.date("%M"));
      
      
      printLog("现在时间是："..hour..":"..minute);
      if hour ~= 12 and hour ~= 21 then
        printLog("现在不是斗技时间");
        
        --调整任务开始时间
        local remainHour = 0;
        if hour > 11 then
          remainHour = 20 - hour;
        else
          remainHour = 11 - hour;
        end
        task.beginTime = os.time() + (60 - minute) * 60 + 60 * 60 * remainHour;
        
      else
        task.beginTime = os.time();
      end
      
      table.insert(timeTaskArray, task);
    end
  end
  
  
  
  if setting["妖气封印_name"] == "0" then
    local times = tonumber(setting["妖气封印_times"]);
    
    --if true then
    --	local times = 1;
    if times ~= 0 then
      local task = {};
      task.name = "妖气封印";
      task.isWorking = false;
      task.times = times;
      task.beginTime = os.time();
      task.teamArray = {};
      
      --sr
      if setting["妖气封印_鬼使黑"] == "0" then
        table.insert(task.teamArray, "鬼使黑");
      end
      
      if setting["妖气封印_骨女"] == "0" then
        table.insert(task.teamArray, "骨女");
      end
      
      if setting["妖气封印_海坊主"] == "0" then
        table.insert(task.teamArray, "海坊主");
      end
      
      if setting["妖气封印_二口女"] == "0" then
        table.insert(task.teamArray, "二口女");
      end
      
      if setting["妖气封印_跳跳哥哥"] == "0" then
        table.insert(task.teamArray, "跳跳哥哥");
      end
      
      --R
      if setting["妖气封印_椒图"] == "0" then
        table.insert(task.teamArray, "椒图");
      end
      
      if setting["妖气封印_饿鬼"] == "0" then
        table.insert(task.teamArray, "饿鬼");
      end
      
      printLog("teamArray是:");
      printTable(task.teamArray);
      
      
      table.insert(taskArray, task);
    end
  end
  
  if setting["个人突破_name"] == "0" then
    local times = tonumber(setting["个人突破_times"]);
    local settingMaxHonor = tonumber(setting["个人突破_maxHonor"])
    local maxHonor = 5;
    if settingMaxHonor == 0 then
      maxHonor = 5;
    elseif settingMaxHonor == 1 then
      maxHonor = 4;
    elseif settingMaxHonor == 2 then
      maxHonor = 3;
    elseif settingMaxHonor == 3 then
      maxHonor = 2;
    end
    
    if times ~= 0 then
      local task = {};
      task.name = "个人突破";
      task.isWorking = false;
      task.times = times;
      task.maxHonor = maxHonor;
      task.beginTime = os.time();
      table.insert(timeTaskArray, task);
    end
  end
  
  if setting["工会突破_name"] == "0" then
    local times = tonumber(setting["工会突破_times"]);
    local settingMaxHonor = tonumber(setting["工会突破_maxHonor"])
    local maxHonor = 5;
    if settingMaxHonor == 0 then
      maxHonor = 5;
    elseif settingMaxHonor == 1 then
      maxHonor = 4;
    elseif settingMaxHonor == 2 then
      maxHonor = 3;
    elseif settingMaxHonor == 3 then
      maxHonor = 2;
    end
    if times ~= 0 then
      local task = {};
      task.name = "工会突破";
      task.isWorking = false;
      task.times = times;
      task.maxHonor = maxHonor;
      task.index = 1;      
      
      if setting["工会突破_beginType"] == "0" then
        --未打过
        task.beginTime = os.time();
        
      elseif setting["工会突破_beginType"] == "1" then
        --已打过
        task.beginTime = os.time() + 10 * 60;
      end
      
      local makeTeam = false;
      if setting["工会突破_makeTeam"] == "0" then
        task.makeTeam = true;
      else
        task.makeTeam = false;
      end
      
      table.insert(timeTaskArray, task);
    end
  end
  
  if setting["空闲时接受邀请_name"] == "0" then
    
    local task = {};
    task.name = "空闲时接受邀请";
    task.isWorking = false;--用来表示任务是否可以被推后
    table.insert(otherTaskArray, task);
  end
  
  
  --判断任务是否创建成功
  
  if next(onlyTaskArray) ~= nil or next(taskArray) ~= nil or next(timeTaskArray) ~= nil or next(otherTaskArray) ~= nil then
    return true;
  else
    return false;
  end
end


--添加一个任务
function addTask(task) 
  
  
end


--对任务进行排序
function sortTaskArray () 
  
  --根据次数排序
  function sortFunc (taskA, taskB) 
    
    local taskATimes = taskA.times;
    local taskBTimes = taskB.times;
    local r = taskATimes < taskBTimes;
    
    return r;
  end
  
  --根据开始时间排序
  function timeSortFuc (taskA, taskB)
    local beginTimeA = taskA.beginTime;
    local beginTimeB = taskB.beginTime;
    local r = beginTimeA < beginTimeB;
    
    return r;
  end
  
  table.sort(taskArray, sortFunc);
  table.sort(timeTaskArray, timeSortFuc);
  
end


--结算 只做封任务
function removeFengTask(task)
  
  --任务可以推后
  task.isWorking = false;
  
  if task.hasTask then
    task.hasTask = false;
    return;
  end
  
  local showStr = "";
  
  --继续查找下一章，直到第一章，结束任务
  local chapterNumber = covertChapterToNumber(task.chapter);
  if chapterNumber > 1 then
    task.chapter = covertNumberToMonsterChapter(chapterNumber - 1);
    task.hasTask = false;
    
    showStr = task.name..",进行到"..task.chapter;
  else
    
    for i,v in ipairs(onlyTaskArray) do
      if v.name == task.name then
        table.remove(onlyTaskArray, i);
      end
    end
    
    showStr = task.name..",已结束。"
    task = nil;
  end
  
  showHUD(infoHUD,showStr,18,"0xffff0000","0xffffffff",0,0,0,300,44);
end


--结算 妖怪发现 任务
function removeFindMonsterTask(task)
  
  --任务可以推后
  task.isWorking = false;
  
  task.times = task.times - 1;
  
  local showStr = "";
  if task.times == 0 then
    showStr = task.name.."任务已全部完成。"
    
    for i,v in ipairs(taskArray) do
      if v.name == task.name then
        table.remove(taskArray, i);
      end
    end
    
    task = nil;
  else
    showStr = task.name.."任务还剩"..task.times.."次";
  end
  
  showHUD(infoHUD,showStr,18,"0xffff0000","0xffffffff",0,0,0,300,44);
end

--移除一个任务
function removeTask(task)
  
  if task.name == "自动石距" then
    --延时一个小时
    task.beginTime = os.time() + 60 * 60;
    task.isWorking = false;
    return;
  end
  
  if task.name == "自动年兽" then
    --延时十二个小时
    task.beginTime = os.time() + 12 * 60 * 60;
    task.isWorking = false;
    return;
  end
  
  
  if task.name == "妖怪发现" then
    return;
  end
  
  if task.times == nil then
    return;
  end
  
  printLog("执行到这里，    remove");
  printTable(task);
  
  task.times = task.times - 1;
  --任务可以推后
  task.isWorking = false;
  
  local showStr = "";
  if task.times <= 0 then
    showStr = task.name.."任务已全部完成。"
    printLog("showStr 删除前");
    
    printTable(taskArray)
    printTable(timeTaskArray)
    for i,v in ipairs(taskArray) do
      if v.name == task.name then
        table.remove(taskArray, i);
      end
    end
    
    for i,v in ipairs(timeTaskArray) do
      if v.name == task.name then
        table.remove(timeTaskArray, i);
      end
    end
    
    printLog("删除后")
    printTable(taskArray)
    printTable(timeTaskArray)
    
    task = nil;
    
  else
    showStr = task.name.."任务还剩"..task.times.."次";
  end
  showHUD(infoHUD,showStr,18,"0xffff0000","0xffffffff",0,0,0,300,44);
  
end


function startTask () 
  
  printLog("开始执行以下任务:");
  printTable(onlyTaskArray);
  printTable(timeTaskArray);
  printTable(taskArray);
  printTable(otherTaskArray);
  
  local lastTaskName = "";
  local lastTask = nil;
  
  while next(onlyTaskArray) ~= nil or next(taskArray) ~= nil or next(timeTaskArray) ~= nil or next(otherTaskArray) ~= nil do 
    
    if isFrontApp(APP_BundleId) == 0 then
      if infoHUD then
        printLog("infoHUD ："..infoHUD);
        hideHUD(infoHUD);
        infoHUD = nil;
      end
      
      --进入下一次循环
      mSleep(5000);
      
    else
      
      local task = nil;
      
      
      --判断上一个任务是否执行完毕，如果没有，则继续执行
      if lastTask ~= nil then
        
        --如果是接受邀请任务，必须要在 主界面才能切换任务（庭院或探索）
        if lastTask.name == "空闲时接受邀请" and lastTask.name == "妖怪发现" then
          if not isPage("庭院")  and not isPage("探索页面") then
            task = lastTask;
          end
        elseif lastTask.isWorking then
          task = lastTask;
        end
      end
      
      --对任务进行排序
      sortTaskArray();
      
      --查询是否有单独执行的任务
      if task == nil then
        if next(onlyTaskArray) ~= nil then
          task = onlyTaskArray[1];
        end
        
      end
      
      --查询是否有定时开始的任务
      if task == nil then
        for i,v in ipairs(timeTaskArray) do
          if v.beginTime <= os.time() then
            task = v;
            break;
          end
        end
      end
      
      
      --如有没有需要执行的定时开始任务，开始第一个正常任务
      if task == nil then
        
        if #taskArray > 0 then
          task = taskArray[1];
          
          --判断该任务是否可以开始执行
          if task.beginTime > os.time() then
            task = nil;
          end
        end
      end
      
      if task == nil then
        if #otherTaskArray > 0 then
          task = otherTaskArray[1];
        else
          task = {};
          task.name = "进行等待";
          task.isWorking = false;
        end
        
        local nearlyTaskName = "未知";
        local nearlyBeginTime = os.time() + 60 * 60 * 24;
        
        --计算下一个任务是什么， 等待时间多久
        if #timeTaskArray > 0 then
          for i,v in ipairs(timeTaskArray) do
            if nearlyBeginTime > v.beginTime then
              nearlyBeginTime = v.beginTime;
              nearlyTaskName = v.name;
            end
          end
        end
        
        if #taskArray > 0 then
          if nearlyBeginTime > taskArray[1].beginTime then
            nearlyBeginTime = taskArray[1].beginTime;
            nearlyTaskName = taskArray[1].name;
          end
        end
        
        task.nextTaskName = nearlyTaskName;
        task.spaceTime = math.ceil((nearlyBeginTime - os.time())/60); 
      end
      
      --提示
      local showStr = "";
      if task.name == "进行等待" then
        showStr = "进行等待，"..task.spaceTime.." 分钟后执行"..task.nextTaskName;
        
      elseif task.name == "空闲时接受邀请" then
        if task.nearlyTaskName == "未知" then
          showStr = "接受邀请";
        else
          showStr = "接受邀请，"..task.spaceTime.." 分钟后执行"..task.nextTaskName;
        end
        
      elseif task.name == "只做封任务" then
        showStr = task.name..",正在查询"..task.chapter;
        
      elseif task.times == nil then
        showStr = task.name;
        
      else
        showStr = task.name.."，倒数"..task.times.."次";
      end
      
      if infoHUD == nil then
        infoHUD = createHUD();
      end
      
      showHUD(infoHUD,showStr,18,"0xffff0000","0xffffffff",0,0,0,300,44);
      
      --准备工作
      printLog("开始执行任务");
      printLog("本次执行的任务是：");
      printTable(task);
      task.isWorking = true;
      
      doTaskPrepareDeal(task, lastTaskName);
      if task.times ~= nil and task.times <= 0 then
        printLog(task.name.."任务已经结束");
      else
        --判断是否还需要执行任务？？？task在其他地方被赋值为nil
        printLog("执行完准备任务之后：");
        printTable(task);
        
        --跳转到合适的页面
        intoSuitablePage(task.name);
        
        
        if task.name == "妖怪发现" then
          妖怪发现(task);
          
        elseif task.name == "空闲时接受邀请" then
          只接受邀请(task);
          
        elseif task.name == "只做封任务" then
          只做封任务(task);
          
        elseif task.name == "个人突破" then
          结界突破 (task,"个人")
          
        elseif task.name == "工会突破" then
          结界突破 (task, "工会")
          
        elseif task.name == "自动石距" or task.name == "自动年兽" or task.name == "妖气封印" then
          挤队伍 (task);
        elseif task.name == "自动斗技" then
          自动斗技(task);
          
        elseif task.name == "八岐大蛇" then
          自动御魂 (task);
          
        elseif task.name == "打业原火" then
          自动御魂 (task)
          
        elseif task.name == "自动觉醒" then
          自动觉醒 (task);
        end
      end
      
      
      
      
      --保存本次任务的名称
      lastTaskName = task.name;
      
      if task.name == "进行等待" or task.name == "空闲时接受邀请" then
        lastTask = nil;
      else
        lastTask = task;
      end
      
      --进入下一次循环
      mSleep(100);
    end
  end
  
  dialogRet("已经完成所有任务。", "确定","","",5);
end
